package com.shuqi.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.database.model.BookInfo;


/**
 * 读写（BookInfo）本地文件
 * 
 * @author linyuren
 * 
 */
public class LocalBookInfoManager {

    private static LocalBookInfoManager mInstance;

    // 搜索模块缓存部分
    private final String mBookSearchRecommendGalleryDataLocalDataName =
            "MBOOKSEARCHRECOMMENDGALLERYDATALOCALDATANAME";

    private final String mBookSearchRecommendSearchLabelDataLocalDataName =
            "MBOOKSEARCHRECOMMENDSEARCHLABELDATALOCALDATANAME";

    /** 搜索排行文件名称 */
    private final String mBookSearchRankLocalDataName = "MBOOKSEARCHRANKLOCALDATANAME";
    /** 搜索男生榜文件名称 */
    private final String mBookSearchManLocalDataName = "MBOOKSEARCH_MAN_LOCALDATANAME";
    /** 搜索女生榜文件名称 */
    private final String mBookSearchWomanLocalDataName = "MBOOKSEARCH_WOMAN_LOCALDATANAME";
    /** 搜索全本文件名称 */
    private final String mBookSearchAllLocalDataName = "MBOOKSEARCH_ALL_LOCALDATANAME";

    // 推荐模块缓存部分
    private final String mRankListFragmentLableLocalDataName =
            "MRANKLISTFRAGMENTLABLELOCALDATANAME";

    private final String mRankListFragmentBookListLocalDataName =
            "MRANKLISTFRAGMENTBOOKLISTLOCALDATANAME";

    private final String mSortFragmentLocalDataName = "MSORTFRAGMENTLOCALDATANAME";

    // 推荐模块（书城）缓存部分
    private final String mSquareRecommendFragmentImgsLocalDataName =
            "MSQUARERECOMMENDFRAGMENTIMGSLOCALDATANAME";

    private final String mSquareaddRecommendFragmentBookInfoLocalDataName =
            "MSQUAREADDRECOMMENDFRAGMENTBOOKINFOLOCALDATANAME";

    private final String mLimitFreeFragmentBookInfoLocalDataName =
            "MLIMITFREEFRAGMENTBOOKINFOLOCALDATANAME";

    private final String mWomanFragmentBookInfoLocalDataName =
            "MWOMANFRAGMENTBOOKINFOLOCALDATANAME";

    private final String mManFragmentBookInfoLocalDataName = "MMANFRAGMENTBOOKINFOLOCALDATANAME";

    private LocalBookInfoManager(Context context) {

    }


    public static synchronized LocalBookInfoManager getInstance() {
        if (mInstance == null) {
            mInstance = new LocalBookInfoManager(ShuqiApplication.getContext());
        }
        return mInstance;
    }


    /** 搜索推荐本地大图数据名称 */
    public String getmBookSearchRecommendGalleryDataLocalDataName() {
        return mBookSearchRecommendGalleryDataLocalDataName;
    }


    /** 搜索推荐标签本地数据名称 */
    public String getmBookSearchRecommendSearchLabelDataLocalDataName() {
        return mBookSearchRecommendSearchLabelDataLocalDataName;
    }

    /** 搜索排行本地数据名称 */
    public String getmBookSearchRankLocalDataName() {
        return mBookSearchRankLocalDataName;
    }

    /** 搜索男生榜文件名称 */
    public String getmBookSearchManLocalDataName() {
        return mBookSearchManLocalDataName;
    }

    /** 搜索女生榜文件名称 */
    public String getmBookSearchWomanLocalDataName() {
        return mBookSearchWomanLocalDataName;
    }

    /** 搜索全本文件名称 */
    public String getmBookSearchAllLocalDataName() {
        return mBookSearchAllLocalDataName;
    }

    /** 获取排行标签中label本地数据名称 */
    public String getmRankListFragmentLableLocalDataName() {
        return mRankListFragmentLableLocalDataName;
    }

    /** 获取排行TAB中排行书籍本地数据名称 */
    public String getmRankListFragmentBookListLocalDataName() {
        return mRankListFragmentBookListLocalDataName;
    }

    /** 获取排行TAB中分类本地数据名称 */
    public String getmSortFragmentLocalDataName() {
        return mSortFragmentLocalDataName;
    }

    /** 书城精品大图 */
    public String getmSquareRecommendFragmentImgsLocalDataName() {
        return mSquareRecommendFragmentImgsLocalDataName;
    }

    /** 书城精品书籍信息 */
    public String getmSquareaddRecommendFragmentBookInfoLocalDataName() {
        return mSquareaddRecommendFragmentBookInfoLocalDataName;
    }

    /** 书城男频书籍信息 */
    public String getmManFragmentBookInfoLocalDataName() {
        return mManFragmentBookInfoLocalDataName;
    }

    /** 书城女频书籍信息 */
    public String getmWomanFragmentBookInfoLocalDataName() {
        return mWomanFragmentBookInfoLocalDataName;
    }


    /** 书城限免 */
    public String getmLimitFreeFragmentBookInfoLocalDataName() {
        return mLimitFreeFragmentBookInfoLocalDataName;
    };

    /** 保存书籍数据 */
    public void saveBookInfo2Local(List<BookInfo> bookInfoData, String localDataName) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            int count = bookInfoData.size();
            dos.writeInt(count);
            for (int i = 0; i < count; i++) {
                BookInfo tempInfo = bookInfoData.get(i);
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookId()) ? "" : tempInfo.getBookId()); // 书籍id
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getSourceId()) ? "" : tempInfo
                        .getSourceId()); // 来源id
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookCoverImgUrl()) ? "" : tempInfo
                        .getBookCoverImgUrl()); // 书籍图片地址
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookName()) ? "" : tempInfo
                        .getBookName()); // 书籍名称
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookAuthorName()) ? "" : tempInfo
                        .getBookAuthorName()); // 书籍作者
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookType()) ? "" : tempInfo
                        .getBookType()); // 书籍类型
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookIntro()) ? "" : tempInfo
                        .getBookIntro()); // 书籍简介

                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookHideState()) ? "" : tempInfo
                        .getBookHideState());// 书籍总控状态位
                dos.writeInt(tempInfo.getCoverHideState());// 书籍封面状态位
            }
            LocalDataFileManager.saveLocalDataFile(localDataName, baos.toByteArray(),
                    ShuqiApplication.getContext());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                    dos = null;
                    baos.close();
                    baos = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /** 读取书籍数据 */
    public List<BookInfo> readLocal2BookInfo(String localDataName) {
        List<BookInfo> localList = new ArrayList<BookInfo>();
        byte[] data =
                LocalDataFileManager
                        .loadLocalDataFile(localDataName, ShuqiApplication.getContext());
        ByteArrayInputStream bais = null;
        DataInputStream dis = null;
        if (data != null) {
            bais = new ByteArrayInputStream(data);
            dis = new DataInputStream(bais);
            try {
                int count = dis.readInt();
                for (int i = 0; i < count; i++) {
                    BookInfo bookInfo = new BookInfo();
                    bookInfo.setBookId(dis.readUTF());// 书籍id
                    bookInfo.setSourceId(dis.readUTF());// 来源id
                    bookInfo.setBookCoverImgUrl(dis.readUTF());// 书籍图片地址
                    bookInfo.setBookName(dis.readUTF());// 书籍名称
                    bookInfo.setBookAuthorName(dis.readUTF());// 书籍作者
                    bookInfo.setBookType(dis.readUTF());// 书籍类型
                    bookInfo.setBookIntro(dis.readUTF());// 书籍简介

                    bookInfo.setBookHideState(dis.readUTF());// 书籍总控状态位
                    bookInfo.setCoverHideState(dis.readInt());// 书籍封面状态位
                    localList.add(bookInfo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    dis.close();
                    dis = null;
                    bais.close();
                    bais = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return localList;
    }



    /** 保存标签数据 */
    public void saveLabelfo2Local(List<BookInfo> bookInfoData, String localDataName) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            int count = bookInfoData.size();
            dos.writeInt(count);
            for (int i = 0; i < count; i++) {
                BookInfo tempInfo = bookInfoData.get(i);
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookId()) ? "" : tempInfo.getBookId());// Id
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookName()) ? "" : tempInfo
                        .getBookName()); // 标签名称
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookCoverImgUrl()) ? "" : tempInfo
                        .getBookCoverImgUrl());// 图片地址
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookType()) ? "" : tempInfo
                        .getBookType());// 书籍类型(某些地方是为了区分是自主广告还是专题大图)
            }
            LocalDataFileManager.saveLocalDataFile(localDataName, baos.toByteArray(),
                    ShuqiApplication.getContext());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                    dos = null;
                    baos.close();
                    baos = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /** 读取标签数据 */
    public List<BookInfo> readLocal2Labelfo(String localDataName) {
        List<BookInfo> localList = new ArrayList<BookInfo>();
        byte[] data =
                LocalDataFileManager
                        .loadLocalDataFile(localDataName, ShuqiApplication.getContext());
        ByteArrayInputStream bais = null;
        DataInputStream dis = null;
        if (data != null) {
            bais = new ByteArrayInputStream(data);
            dis = new DataInputStream(bais);
            try {
                int count = dis.readInt();
                for (int i = 0; i < count; i++) {
                    BookInfo bookInfo = new BookInfo();
                    bookInfo.setBookId(dis.readUTF());
                    bookInfo.setBookName(dis.readUTF());// 标签名称
                    bookInfo.setBookCoverImgUrl(dis.readUTF());
                    bookInfo.setBookType(dis.readUTF());
                    localList.add(bookInfo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    dis.close();
                    dis = null;
                    bais.close();
                    bais = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return localList;
    }



    /** 保存书城数据（推荐TAB） */
    public void saveBookCityBookInfo2Local(List<BookInfo> bookInfoData, String localDataName) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            int count = bookInfoData.size();
            dos.writeInt(count);
            for (int i = 0; i < count; i++) {
                BookInfo tempInfo = bookInfoData.get(i);

                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookId()) ? "" : tempInfo.getBookId()); // 书籍id
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookCoverImgUrl()) ? "" : tempInfo
                        .getBookCoverImgUrl()); // 书籍图片地址
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookName()) ? "" : tempInfo
                        .getBookName()); // 书籍名称
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookAuthorName()) ? "" : tempInfo
                        .getBookAuthorName()); // 书籍作者
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookType()) ? "" : tempInfo
                        .getBookType()); // 书籍类型
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookIntro()) ? "" : tempInfo
                        .getBookIntro()); // 书籍简介

                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookHideState()) ? "" : tempInfo
                        .getBookHideState());// 书籍总控状态位
                dos.writeInt(tempInfo.getCoverHideState());// 书籍封面状态位
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getBookStatus()) ? "" : tempInfo
                        .getBookStatus());// 书籍状态（此处保存的是颜色值）
                dos.writeUTF(TextUtils.isEmpty(tempInfo.getDiscount()) ? "" : tempInfo
                        .getDiscount()); //
            }
            LocalDataFileManager.saveLocalDataFile(localDataName, baos.toByteArray(),
                    ShuqiApplication.getContext());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                    dos = null;
                    baos.close();
                    baos = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /** 读取书城数据 */
    public List<BookInfo> readBookCityLocal2BookInfo(String localDataName) {
        List<BookInfo> localList = new ArrayList<BookInfo>();
        byte[] data =
                LocalDataFileManager
                        .loadLocalDataFile(localDataName, ShuqiApplication.getContext());
        ByteArrayInputStream bais = null;
        DataInputStream dis = null;
        if (data != null) {
            bais = new ByteArrayInputStream(data);
            dis = new DataInputStream(bais);
            try {
                int count = dis.readInt();
                for (int i = 0; i < count; i++) {
                    BookInfo bookInfo = new BookInfo();
                    bookInfo.setBookId(dis.readUTF());// 书籍id
                    bookInfo.setBookCoverImgUrl(dis.readUTF());// 书籍图片地址
                    bookInfo.setBookName(dis.readUTF());// 书籍名称
                    bookInfo.setBookAuthorName(dis.readUTF());// 书籍作者
                    bookInfo.setBookType(dis.readUTF());// 书籍类型
                    bookInfo.setBookIntro(dis.readUTF());// 书籍简介

                    bookInfo.setBookHideState(dis.readUTF());// 书籍总控状态位
                    bookInfo.setCoverHideState(dis.readInt());// 书籍封面状态位
                    bookInfo.setBookStatus(dis.readUTF());// 书籍状态（此处保存的是颜色值）
                    bookInfo.setDiscount(dis.readUTF());
                    localList.add(bookInfo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    dis.close();
                    dis = null;
                    bais.close();
                    bais = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return localList;
    }
}
